/**
 * get请求
 * @param {String} url 
 * @param {Object} params 
 * @param {String} type 
 * @param {Object} readers 
 * @returns 
 */
function get(url,params,type,readers={}){
    const p = new Promise((resolve,reject)=>{
        const xhr = new XMLHttpRequest
        xhr.onreadystatechange=function(){
            if(xhr.readyState===4){
                if(xhr.status===200){
                    let res = xhr.responseText
                    if(type==='json')  res = JSON.parse(res)
                    resolve(res)
                }else{
                    
                    reject(xhr.status)
                }
            }
        }
        //过滤params
        let temp = []
        for(let key in params){
            temp.push(`${key}=${params[key]}`)
        }
        let paramsFilter = temp.join('&')
        xhr.open('get',`${url}?${paramsFilter}`)
        //过滤readers
        for(let key in readers){
            xhr.setRequestHeader(key,params[key])
        }
        xhr.send(null)
    })
    return p 
}


/**
 * post请求
 * @param {String} url 
 * @param {Object} params 
 * @param {String} type 
 * @param {Object} readers 
 * @returns 
 */
 function post(url,params,type,readers={}){
    const p = new Promise((resolve,reject)=>{
        const xhr = new XMLHttpRequest
        xhr.onreadystatechange=function(){
            if(xhr.readyState===4){
                if(xhr.status===200){
                    let res = xhr.responseText
                    if(type==='json')  res = JSON.parse(res)
                    resolve(res)
                }else{
                    
                    reject(xhr.status)
                }
            }
        }
        //过滤params
        let temp = []
        for(let key in params){
            temp.push(`${key}=${params[key]}`)
        }
        let paramsFilter = temp.join('&')
        xhr.open('post',url)
        xhr.setRequestHeader('content-type','application/x-www-form-urlencoded')
        //过滤readers
        for(let key in readers){
            xhr.setRequestHeader(key,params[key])
        }
        xhr.send(paramsFilter)
    })
    return p 
}